// firstPackage/pages/busJoinHands/busJoinHands.js
import { businessCreate, queryConsultInfo } from "../../../service/user_service"
import debounce from "../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    showSuccessPop: false,
    isLogin: false,
    list: [
      {
        title: "楼盘代理",
        desc: "立足川渝，布局全国",
        icon: "https://saas.img.ylyc.top/icon/bqddl_icon.png",
        isChoose: true,
      },
      {
        title: "渠道合作",
        desc: "上千家合作经纪公司",
        icon: "https://saas.img.ylyc.top/icon/bqdzh_icon.png",
        isChoose: false,
      },
      {
        title: "项目包销",
        desc: "资金实力雄厚，操盘经验丰富",
        icon: "https://saas.img.ylyc.top/icon/bxmbx_icon.png",
        isChoose: false,
      },
      {
        title: "小程序开发",
        desc: "研发小程序 APP 产品推广",
        icon: "https://saas.img.ylyc.top/icon/bxcxyf_icon.png",
        isChoose: false,
      },
      {
        title: "其他合作",
        desc: "其他意向合作",
        icon: "https://saas.img.ylyc.top/icon/bqthz_icon.png",
        isChoose: false,
      }
    ],
    formData: {
      businessType: "楼盘代理",
      name: "",
      phone: "",
      companyName: "",
      projectName: "",//项目名
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'formData.name': app.globalData.name || app.globalData.nickName,
      'formData.phone': app.globalData.phone || "",
      isLogin: app.globalData.isLogin
    })

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //选择类型
  selectTagItem(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.list;
    arr.forEach((i, idx) => {
      i.isChoose = false;
      if (idx == index) {
        i.isChoose = true
        this.setData({
          'formData.businessType': i.title
        })
      }
    })
    this.setData({
      list: arr
    })
  },
  //输入绑定
  inputCellChange(e) {
    // console.log("JJLog:e==:" + JSON.stringify(e));
    let detail = e.detail;
    let changeItem = `formData.${detail.key}`;
    this.setData({
      [changeItem]: detail.value
    })
  },


  //提交商务合作:
  submitAppointmentAction() {

    let data = this.data.formData;
    if (!data.name) {
      wx.showToast({
        title: '请输入您的姓名',
        icon: "none"
      })
      return;
    }
    if (!data.phone) {
      wx.showToast({
        title: '请输入您的电话',
        icon: "none"
      })
      return;
    }
    debounce(this.createBusRequest);
  },
  //提交商务请求:
  createBusRequest() {
    wx.showLoading();
    businessCreate(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showSuccessPop: true
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
        icon: "none"
      })
      console.error("JJLog:createBusRequest", err);
    })
  },

  askServiceInfo() {
    wx.showLoading();
    queryConsultInfo(this.data.formData.businessType).then(res => {
      wx.hideLoading();
      console.log("JJLog:查询返回:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data && res.data.phone) {
          wx.makePhoneCall({
            phoneNumber: res.data.phone,
            fail: err => {

            }
          })
        } else {
          wx.showToast({
            title: "暂时无法咨询,请联系客服处理",
            icon: "none"
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryConsultInfo", err);
    })
  },

  //确认点击
  successPopSure() {
    this.setData({
      showSuccessPop: false
    })
    wx.navigateBack({
      delta: 0,
    })
  },




})